#pragma once
#ifndef TIMER_H
#define TIMER_H
#include <chrono>

namespace solex {

// 计时器类，使用C++标准库中的<chrono>来测量时间
// Timer class, using <chrono> from the C++ Standard Library for measuring time
class Timer {
public:
    // 构造函数，初始化计时器的起始时间
    // Constructor, initializes the start time of the timer
    Timer() : start_time_(std::chrono::high_resolution_clock::now()) {}

    // 获取自计时器创建或上次更新以来的经过时间（以毫秒为单位）
    // Gets the elapsed time (in milliseconds) since the timer was created or last updated
    double GetCostTime() const {
        auto end_time = std::chrono::high_resolution_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time_);
        return duration.count();
    }

    // 更新计时器的起始时间到当前时间，并返回自上次更新以来经过的时间（以毫秒为单位）
    // Updates the timer's start time to the current time, and returns the elapsed time (in milliseconds) since the last update
    double GetCostTimeUpdate() {
        auto end_time = std::chrono::high_resolution_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time_).count();
        start_time_ = std::chrono::high_resolution_clock::now();
        return duration;
    }

private:
    // 计时器的起始时间点
    // Start time point of the timer
    std::chrono::time_point<std::chrono::high_resolution_clock> start_time_;
};

}   // namespace solex

#endif // TIMER_H
